package org.execute.tool.文件处理;
/**
 * @author ：lk
 * @date ：Created in 2020/4/29 16:41
 * @description：文件行处理
 * @modified By：
 * @version: $
 */

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;

public class FileLineProcessor {
    /**
     * 行替换 副本
     * @param filePath
     */
    public static void processLinesInFile(String filePath) {
        String outputFilePath = filePath + ".processed";
        try (BufferedReader reader = new BufferedReader(new FileReader(filePath));
             BufferedWriter writer = new BufferedWriter(new FileWriter(outputFilePath))) {
            String line;
            while ((line = reader.readLine()) != null) {
                // 去除行尾的换行符
                line = line.trim();

                // 替换特定子字符串
                line = line.replace("https://www.socialboosting.com/", "/");
                line = line.replace(".html", "");
                // 将处理后的行写入副本文件
                writer.write(line);
                writer.newLine();  // 写入换行符
            }
        } catch (IOException e) {
            System.err.println("处理文件时发生错误：" + e.getMessage());
        }
    }


    public static void main(String[] args) {
        ArrayList<String> strings = ReadTheFileToGenerateAnArray.readTheFileToGenerateAnArray("E:\\文件数组.txt");
        // 处理文件
        for (String string : strings) {
            processLinesInFile(string);
        }

    }
}
